//
//  PersonalInfomationVC.m
//  Recycling
//
//  Created by jzd on 2018/5/18.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "PersonalInfomationVC.h"
#import "selectDateVC.h"
#import "transitionAnnimater.h"

@interface PersonalInfomationVC ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomCons;
@property (weak, nonatomic) IBOutlet UIButton *defaultSex;
@property (weak, nonatomic) IBOutlet UIScrollView *bottomScroll;
@property (weak, nonatomic) IBOutlet UITextField *nameTextF;
@property (weak, nonatomic) IBOutlet UIButton *girlBut;
@property (weak, nonatomic) IBOutlet UITextField *telTextF;
@property (weak, nonatomic) IBOutlet UITextField *userIdCar;
@property (weak, nonatomic) IBOutlet UILabel *birthday;
@property (weak, nonatomic) IBOutlet UITextField *liveAddress;
@property (weak, nonatomic) IBOutlet UITextField *currentAddress;

@property (strong,nonatomic) UIButton *currentSex;

@property(strong,nonatomic)transitionAnnimater *transitionVC;
@end

@implementation PersonalInfomationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.currentSex = self.defaultSex;
    self.view.backgroundColor = BGCOLOR;
    self.navigationItem.title = @"个人信息";
    self.bottomCons.constant = SJScreenH-430;
    //监听键盘的弹出
    [self autoScrollViewContentSize];
    
    //请求数据
    [self getMineMessage];
}
-(transitionAnnimater *)transitionVC{
    if (_transitionVC == nil) {
        _transitionVC = [[transitionAnnimater alloc]init];
        _transitionVC.torect = CGRectMake(0, SJScreenH-260, SJScreenW, 260);
    }
    return _transitionVC;
}
-(void)autoScrollViewContentSize{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardHidn:) name:UIKeyboardDidHideNotification object:nil];
}
-(void)keyBoardShow:(NSNotification *)noti{
    if (iphone5) {
        self.bottomScroll.contentSize = CGSizeMake(0, SJScreenH+130);
    }else if (iphone6){
        self.bottomScroll.contentSize = CGSizeMake(0, SJScreenH+80);
    }
    
}
-(void)keyBoardHidn:(NSNotification *)noti{
    self.bottomScroll.contentSize = CGSizeMake(0, SJScreenH);
    [UIView animateWithDuration:0.2 animations:^{
        self.bottomScroll.contentOffset = CGPointMake(0, -(statsBarHeight+NavgationBarH));
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

/**
 请求个人信息
 */
-(void)getMineMessage{
    NSDictionary *params = @{@"uid":@([UserInfoTool shareUserInfoTool].Info.ID)};
    [[SJNetWorkTool sharedTool]requestWithURLString:mineUserInfo parameters:params method:GET callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    self.nameTextF.text = [data objectForKey:@"UserName"];
                    self.userIdCar.text = [data objectForKey:@"UserIDCard"];
                    self.telTextF.text = [data objectForKey:@"UserTel"];
                    UIButton *but = ([[data objectForKey:@"UserSex"]isEqualToString:@"1"])?self.defaultSex:self.girlBut;
                    [self selectsex:but];
                    self.birthday.text = [NSString getRegureTime:[data objectForKey:@"BirthDay"]];
                    self.liveAddress.text = [data objectForKey:@"UserAddr"];
                    self.currentAddress.text = [data objectForKey:@"NowLiveAddr"];
                }else{
                    [self.navigationController.view showToast:@"空数据" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
            
        }
    }];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.bottomScroll endEditing:YES];
    [[SJNetWorkTool sharedTool].tasks makeObjectsPerformSelector:@selector(cancel)];
}

- (IBAction)selectsex:(UIButton *)sender {
    if (sender == self.currentSex) {
        return;
    }
    sender.selected = YES;
    self.currentSex.selected = NO;
    self.currentSex = sender;
}

- (IBAction)scrollViewOnClick:(UITapGestureRecognizer *)sender {
    [self.bottomScroll endEditing:YES];
}

/**
 保存个人信息
 */
- (IBAction)saveButtonOnClick:(UIButton *)sender {
    [self.bottomScroll endEditing:YES];
    if (self.nameTextF.text.length == 0) {
        [self.navigationController.view showToast:@"请输入姓名" completion:nil];
        return;
    }
    if (self.telTextF.text.length == 0) {
        [self.navigationController.view showToast:@"请输入手机号" completion:nil];
        return;
    }
    
    NSInteger sex = ([self.currentSex.titleLabel.text isEqualToString:@"男"])?1:0;
    NSString *selectSex = [NSString stringWithFormat:@"%ld",sex];
    NSDictionary *dic = @{@"UserID":@([UserInfoTool shareUserInfoTool].Info.ID),
                             @"UserName":self.nameTextF.text,
                             @"UserTel":self.telTextF.text,
                             @"UserSex":selectSex
                             };
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dic];
    
    if (self.userIdCar.text.length != 0) {
        [params setObject:self.userIdCar.text forKey:@"UserIDCard"];
    }
    if (self.birthday.text.length != 0) {
        [params setObject:self.birthday.text forKey:@"BirthDay"];
    }
    if (self.liveAddress.text.length != 0) {
        [params setObject:self.liveAddress.text forKey:@"UserAddr"];
    }
    if (self.currentAddress.text.length != 0) {
        [params setObject:self.currentAddress.text forKey:@"NowLiveAddr"];
    }
    
    [[SJNetWorkTool sharedTool]requestWithURLString:saveUserInfo parameters:params.copy method:POST callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            [self.navigationController.view showToast:msg completion:nil];
            if (code == 1) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }];
    
}
- (IBAction)selectBirthday:(UIButton *)sender {
    [self.bottomScroll endEditing:YES];
    selectDateVC *vc = [[selectDateVC alloc]init];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.transitioningDelegate  = self.transitionVC;
    __block __weak PersonalInfomationVC *copy_self = self;
    vc.dateBlock = ^(NSString *date) {
        copy_self.birthday.text = date;
    };
    [self presentViewController:vc animated:YES completion:nil];
}




-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
